package org.apache.ibatis.common.type;

import org.apache.ibatis.common.annotations.Many;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Name JdbcType
 * @Description: 所有的 JDBC 类型（来源于 java.sql.Types）
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-05-09 16:33:50
 **/
public enum JdbcType {
    ARRAY(Types.ARRAY),     // 添加它是为了启用对 ARRAY 数据类型的支持，但是需要自定义类型处理器。
    BIT(Types.BIT),
    TINIINT(Types.TINYINT),
    SMALLINT(Types.SMALLINT),
    INTEGER(Types.INTEGER),
    BIGINT(Types.BIGINT),
    FLOAT(Types.FLOAT),
    REAL(Types.REAL),
    DOUBLE(Types.DOUBLE),
    NUMERIC(Types.NUMERIC),
    DECIMAL(Types.DECIMAL),
    CHAR(Types.CHAR),
    VARCHAR(Types.VARCHAR),
    LONGVARCHAR(Types.LONGVARCHAR),
    DATE(Types.DATE),
    TIME(Types.TIME),
    TIMESTAMP(Types.TIMESTAMP),
    BINARY(Types.BIGINT),
    VARBINARY(Types.VARBINARY),
    LONGVARBINARY(Types.LONGVARBINARY),
    NULL(Types.NULL),
    OTHER(Types.OTHER),
    BLOB(Types.BLOB),
    CLOB(Types.CLOB),
    BOOLEAN(Types.BOOLEAN),
    CURSOR(-10),    // ORACLE
    UNDEFINED(Integer.MIN_VALUE + 1000),
    NVARCHAR(Types.NVARCHAR),   // JDK 6
    NCHAR(Types.NCHAR),     // JDK 6
    NCLOB(Types.NCLOB),     // JDK 6
    STRUCT(Types.STRUCT),
    JAVA_OBJECT(Types.JAVA_OBJECT),
    DISTINCT(Types.DISTINCT),
    REF(Types.REF),
    DATALINK(Types.DATALINK),
    ROWID(Types.ROWID),     // JDK 6
    LONGNVARCHAR(Types.LONGNVARCHAR),   // JDK 6
    SQLXML(Types.SQLXML),   // JDK 6
    DATETIMEOFFSET(-155),   // SQL SERVER 2008
    TIME_WITH_TIMEZONE(Types.TIME_WITH_TIMEZONE),   // JDBC 4.2 & JDK 8
    TIMESTAMP_WITH_TIMEZONE(Types.TIMESTAMP_WITH_TIMEZONE);     // JDBC 4.2 & JDK 8

    public final int TYPE_CODE;
    private static Map<Integer, JdbcType> codeLookup = new HashMap<>();

    static {
        for (JdbcType type : JdbcType.values()) {
            codeLookup.put(type.TYPE_CODE, type);
        }
    }

    JdbcType(int code) {
        this.TYPE_CODE = code;
    }

    public static JdbcType forCode(int code) {
        return codeLookup.get(code);
    }
}
